﻿CREATE PROCEDURE [dbo].[proc_Customer_GetShiftList_Count]
	(
		@DepartmentId int,
		@Stext nvarchar(50)
	)
AS
--	;WITH ChildDepartment(ID,DName,PId) AS
--	(
--		SELECT ID,DName,ParentID FROM department WHERE id=@departmentid
--	    
--		UNION ALL
--		SELECT department.ID,department.DName,department.ParentID FROM ChildDepartment AS d
--		INNER JOIN department ON d.ID = department.ParentID
--	)
	Declare @bmbh_T  varchar(30)
	Select @bmbh_T=bmbh From department Where id=@DepartmentId
	Select Count(Id) From Customer C
	where
			(select e.departmentid from employee e where e.username=C.ProtectName) in(Select Id From department Where Left(bmbh,Len(@bmbh_T))=@bmbh_T) 
			 and ((select e.ename from employee e where e.username=C.ProtectName) like  '%'+@SText+'%' or C.CustomerName like '%'+@SText+'%')
			 and C.ProtectId=2
